package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.ICategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public Page<Category> findByPage(Integer pageNum, Integer pageSize) {
        // 1.先查套餐基本信息
        // 1-1 构建条件对象
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        // 1-2 构建分页对象
        Page<Category> page = new Page<>(pageNum, pageSize);
        // 1-3 查询
        page = categoryMapper.selectPage(page,categoryWrapper);

        // 5.返回结果
        return page;
    }

    @Override
    public void saveCategory(Category category) {
        // 1.补齐参数
        // 1-1 id
        long id = IdUtil.getSnowflake(1, 1).nextId();
        category.setId(id);
//        // 1-2 创建、更新时间
//        category.setCreateTime(new Date());
//        category.setUpdateTime(new Date());
//        // 1-3 创建、更新人
//        category.setCreateUser(1L); // 暂时写死1L
//        category.setUpdateUser(1L);// 暂时写死1L

        // 2.调用mapper新增
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(Category category) {
//            // 1.补齐参数
//            category.setUpdateTime(new Date());
//            category.setUpdateUser(1L);// 暂时写死

            // 2.调用mapper修改
            categoryMapper.updateById(category);
        }

    @Override
    public List<Category> findByType(Integer type) {
        // 1.构建查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,type );
        // 2.调用mapper查询
        return categoryMapper.selectList(wrapper);
    }

    @Override
    public List<Category> findAll() {
        // 1.构建查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        return categoryMapper.selectList(wrapper);
    }
}

